package com.clip.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.clip.entity.User;
import com.clip.entity.UserEmailEvent;


@Repository
public class UserEmailEventDAO extends BasicDao{
	
	private static final Log log = LogFactory.getLog(UserEmailEvent.class);
	
	public int save(UserEmailEvent transientInstance) {
        log.debug("saving UserEmailEvent instance");
        try {
            int flag= (Integer)getSession().save(transientInstance);
            log.debug("save successful");
            return flag;
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
	public void update(UserEmailEvent transientInstance){
		log.debug("update UserEmailEvent instance");
        try {
            getSession().update(transientInstance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
	}
	public void delete(UserEmailEvent transientInstance){
		log.debug("delete UserEmailEvent instance");
        try {
            getSession().delete(transientInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
	}
	
	public List<UserEmailEvent> findUserEmailEventByObj(User user,String obj,Integer type){
		String hql = "SELECT uel FROM UserEmailEvent uel WHERE uel.uid="+user.getId()+" AND uel.type="+type+" AND uel.obj='"+obj+"'";
		return getSession().createQuery(hql).list();
	}
	public UserEmailEvent checkResetAuthorize(String activate,Integer type,Boolean timer){
		UserEmailEvent uel = null;
		Date date = new Date();
		Long time = 1L + 1*24*60*60*1000;
		String hql = "SELECT uel FROM UserEmailEvent uel WHERE uel.code='"+activate+"' AND uel.type="+type+" AND uel.status=0"+(timer ? (" AND uel.actDate>'"+(date.getTime()-time)+"'") : "");
		List<UserEmailEvent> list = getSession().createQuery(hql).list();
		uel = list!=null&&list.size()>0 ? list.get(0) : null;
		return uel;
	}
	public void executeQuery(String hql){
		getSession().createQuery(hql).executeUpdate();
	}
	
	public Object findByQueryWithUniqueResult(String hql,Object... args){
		Query query=getSession().createQuery(hql);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
		}
		return query.uniqueResult();
	}
	
	public List findByQuery(String hql){
		return findByQuery(hql,null);
	}
	
	public List findByQuery(String hql,Object... args){
		Query query=getSession().createQuery(hql);
		if(args!=null&&args.length>0){
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
		}
		return query.list();
	}
}
